package com.qingfeng.medical.controller.front.message;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.domain.entity.BaseEntity;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.biz.message.domain.entity.NewsNotify;
import com.qingfeng.medical.biz.message.domain.enums.IsSeeEnum;
import com.qingfeng.medical.biz.message.domain.enums.UserTypeEnum;
import com.qingfeng.medical.biz.message.service.NewsNotifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 消息通知表
 *
 * @author 清风学Java
 * @date 2023-05-03 22:53:02
 */
@Api(tags = "消息通知表管理")
@RestController
@RequestMapping("/api/info/news")
@CrossOrigin
@Validated
public class NewsNotifyApiController  extends BaseController {

    @Autowired
    private NewsNotifyService newsNotifyService;

    @ApiOperation(value = "根据用户Id消息分页列表", notes = "根据用户Id消息分页列表")
    @GetMapping("/list")
    @Log(title = "前台消息通知管理", businessType = BusinessType.SELECT, message = "根据用户Id消息分页列表")
    public Result<Page<NewsNotify>> newsNotifyList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @RequestParam("page") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @RequestParam("limit") Long limit,
            @ApiParam(name = "userId", value = "用户Id", required = true)
            @RequestParam("userId") Long userId) {
        Page<NewsNotify> newsNotifyPage = newsNotifyService.page(
                new Page<NewsNotify>(page, limit),
                new LambdaQueryWrapper<NewsNotify>()
                        .eq(NewsNotify::getUserId, userId)
                        .eq(NewsNotify::getUserType, UserTypeEnum.FRONT)
                        .orderByAsc(NewsNotify::getIsSee)
                        .orderByDesc(BaseEntity::getCreateTime)
        );
        return Result.ok(newsNotifyPage);
    }

    /**
     * 修改查看状态
     * @param id
     * @return
     */
    @ApiOperation(value = "消息通知表信息", notes = "消息通知表信息")
    @GetMapping("/info/{id}")
    @Log(title = "消息通知表", businessType = BusinessType.SELECT, message = "消息通知表信息")
    public Result newsNotifyInfo(@PathVariable("id") Long id) {
        NewsNotify newsNotify = newsNotifyService.getById(id);
        if (newsNotify.getIsSee().equals(IsSeeEnum.IS_NOT_VIEWED)) {
            newsNotify.setIsSee(IsSeeEnum.IS_VIEWED);
            newsNotifyService.updateById(newsNotify);
        }
        return Result.ok();
    }



}
